import { message, notification } from 'ant-design-vue'
import {
	ConfigProps,
	NotificationApi,
	NotificationArgsProps,
} from 'ant-design-vue/es/notification'

enum PlaceMentEnum {
	topLeft = 'topLeft',
	topRight = 'topRight',
	bottomLeft = 'bottomLeft',
	bottomRight = 'bottomRight',
}

enum NotificationTypes {
	open = 'open',
	success = 'success',
	error = 'error',
	info = 'info',
	warning = 'warning',
	warn = 'warn',
	close = 'close',
	destroy = 'destroy',
}

type NotificationConfigType = NotificationArgsProps | string

interface NotificationInstance {
	defaultConfig: ConfigProps | undefined
	defaultNotificationProps: NotificationArgsProps
}

/**
 * @description 闭包形式，判断是否已经全局初始化Notification
 */
let isInit = false

/**
 * @description Notification的默认配置
 */
const defaultNotificationConfig: ConfigProps = {
		duration: 2,
		placement: PlaceMentEnum.topRight,
		top: '10px',
	},
	defaultNotificationProps: NotificationArgsProps = {
		message: '提示',
	}

function ExecuteMethod(
	this: any,
	config: NotificationConfigType,
	type: NotificationTypes,
) {
	const totalConfig =
		typeof config == 'string'
			? Object.assign({}, this.defaultNotificationProps, {
					description: config,
			  })
			: Object.assign({}, this.defaultNotificationProps, config)
	this.instance[type](totalConfig)
}

/**
 * @description Notification方法类
 */
class InitNotificationMethod implements NotificationApi {
	instance: NotificationApi | undefined

	open(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.open)
	}
	success(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.success)
	}
	error(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.error)
	}
	info(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.info)
	}
	warning(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.warning)
	}
	warn(config: NotificationConfigType) {
		ExecuteMethod.call(this, config, NotificationTypes.warn)
	}
	close(key: string) {
		if (!key) {
			message.error('请输入notification的key值！')
			return
		}
		notification.close(key)
	}
	destroy() {
		notification.destroy()
	}
	config(globalConfig: ConfigProps) {
		notification.config(globalConfig)
	}
}

/**
 * @description Notification实例类
 */
class Notification
	extends InitNotificationMethod
	implements NotificationInstance
{
	defaultConfig: ConfigProps | undefined
	defaultNotificationProps: NotificationArgsProps
	constructor(initParams?: ConfigProps, initProps?: NotificationArgsProps) {
		super()
		this.defaultNotificationProps = {
			...defaultNotificationProps,
			...initProps,
		}
		if (!isInit) {
			this.instance = notification
			this.defaultConfig = { ...defaultNotificationConfig, ...initParams }
			this.config(this.defaultConfig)
			isInit = true
		}
	}
}

export const openNotification = new Notification()
